package com.unicom.projt.fee;

import android.content.Intent;
import android.util.Log;
import com.unicom.projt.Const;
import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-14
 * Time: 下午3:05
 */
public class PaymentController extends AbstractHttpController<String> {
    private PaymentPage paymentPage;
    private String phoneNumber;
    private Double amount;


    public PaymentController(PaymentPage paymentPage) {
        super(String.class);
        this.paymentPage = paymentPage;
    }

    public void sendPaymentInfo(String phoneNumber,String cardPwd, Double amount) {
        this.phoneNumber = phoneNumber;
        this.amount = amount;
        Double tempAmount = amount * 100;
        String svcCont = "<SvcCont><TelNo>" + phoneNumber + "</TelNo><Type>2</Type>" +
                "<Password>"+cardPwd+"</Password><Amout>" + tempAmount.intValue() + "</Amout></SvcCont>";
        String postXml = new PostXmlBuilder().setBiPCode("100").setTelNo(UserHolder.phoneNumber).setSvcCont(svcCont).setActionID("1").createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    @Override
    public void onRequestFinish(String response) {
        paymentPage.hideProgressDialog();
        if (response == null || "".equals(response)) {
            paymentPage.hideProgressDialog();
            ToastUtils.showMessage(paymentPage, "错误，请稍候再试");
            return;
        }

        String respCode = SimpleXmlParser.getResponseCode(response);
        if (ResponseCode.SUCCESS.equals(respCode)) {
            String orderId=SimpleXmlParser.getOrderId(response);
            String payPkg=SimpleXmlParser.getPayPkg(response);
            Log.d(Const.TAG,payPkg);
            Intent intent = new Intent(paymentPage, PaymentConfirmPage.class);
            intent.putExtra("amount", amount);
            intent.putExtra("phoneNumber", phoneNumber);
            intent.putExtra("orderId", orderId);
            intent.putExtra("payPkg", payPkg);
            intent.putExtra("isIvrPay", false);
            paymentPage.startActivity(intent);
            paymentPage.finish();
        } else {
            ToastUtils.showMessage(paymentPage, "订单生成失败");
        }

    }

    @Override
    public void onNetworkAbort() {
        paymentPage.hideProgressDialog();
        ToastUtils.showMessage(paymentPage, "错误，网络连接被中断.");
    }

    @Override
    public void onNetworkError() {
        paymentPage.hideProgressDialog();
        ToastUtils.showMessage(paymentPage, "错误，网络连接错误.");
    }

    @Override
    public void onResponseError() {
        paymentPage.hideProgressDialog();
        ToastUtils.showMessage(paymentPage, "错误，返回结果错误.");
    }
}
